import { IFeidaoAiWeappPage } from '@dfeidao/atom-wxapp/interfaces';
import nodejs from '@dfeidao/atom-wxapp/msg/nodejs';
import render from '@dfeidao/atom-wxapp/render/render';
import { spaceid, file_url } from '../atom/config';
import hide_loading from '@dfeidao/atom-wxapp/api/interaction/hide-loading';
import show_loading from '@dfeidao/atom-wxapp/api/interaction/show-loading';

interface IGoodsCategory {	// // 商品类别goods_category
	category_num: string;	// 类别编号
	category_name: string;	// 类别名称
	superior_category_number: string;	// 上级类别编号
	level: string;	// 层级
	fzno: string;	// 顺序号
	is_stop: number;	// 是否停用（是1、否0）
	memo: string;	// 备注
}
interface ICommoditySale { 	// 上架商品commodity_sale
	commodity_no: string;	// 商品编号
	commodity_sale_no: string;	// 上架商品编号
	category_num: string;	// 类别编号
	category_name: string;	// 类别名称;
	commodity_thumbnails: string;	// 缩略图
	img: string;
}
interface ICommodity extends IGoodsCategory {
	arr: ICommoditySale[];
}
export default async function a001(fd: IFeidaoAiWeappPage) {
	// 初始化
	show_loading('加载中', true);
	try {
		const types = await nodejs<IGoodsCategory[]>('app-commodity-type/s002', { filter: { level: 1 } });

		const hide = (() => {
			if (types.length === 0) {
				return true;
			} else {
				return false;
			}
		})();
		if (hide) {
			render(fd, {
				hide
			});
			return;
		}


		const category_num = types[0].category_num;
		const goods_res = await nodejs<ICommodity[]>('app-commodity-type/s003', { filter: { superior_category_number: category_num } });

		const goods = (() => {
			if (goods_res.length === 0) {
				return [];
			} else {
				return goods_res.map((item) => {
					const _arr = item.arr;
					if (_arr) {
						_arr.forEach((i) => {
							i.img = file_url + 'getfile?productid=' + spaceid + '&id=' + i.commodity_thumbnails;
						});
					}
					return item;
				});
			}
		})();

		render(fd, { types, goods, select: 0 });
		hide_loading();

	} catch (error) {
		hide_loading();
	}
}
